package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetMealService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private SetMealService setMealService;

    @Reference
    private ReportService reportService;

    //给会员统计折线图提供数据
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){

        Map<String,Object> map = new HashMap<String, Object>();

        //封装months参数
        List<String> months = new ArrayList<>();
     /*   months.add("2019.1");
        months.add("2019.2");
        months.add("2019.3");
        months.add("2019.4");
        map.put("months",months);*/
     //获取当前的日期
        Calendar calendar = Calendar.getInstance();
        //获取12个月之前日期
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
           months.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        map.put("months",months);

        //封装memberCount参数
        /*List<Integer> memberCount = new ArrayList<>();
        memberCount.add(3);
        memberCount.add(5);
        memberCount.add(8);
        memberCount.add(10);
        map.put("memberCount",memberCount);*/
        try {
            List<Integer> memberCount = memberService.findUserCountByCondition(months);
            map.put("memberCount",memberCount);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }


    }

    //给套餐统计饼型图提供数据
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){

        Map<String,Object> map = new HashMap<>();
        //查询套餐名称
        /*List<String> setmealNames = new ArrayList<>();
        setmealNames.add("直接访问");
        setmealNames.add("邮件营销");
        setmealNames.add("联盟广告");
        setmealNames.add("视频广告");
        setmealNames.add("搜索引擎");
        map.put("setmealNames",setmealNames);*/
        //查询套餐名称及数量
        /*List<Map> setmealCount = new ArrayList<>();
        Map<String,Object>map1 = new HashMap<>();
        map1.put("value",335);
        map1.put("name","直接访问");
        setmealCount.add(map1);
        Map<String,Object>map2 = new HashMap<>();
        map2.put("value",310);
        map2.put("name","邮件营销");
        setmealCount.add(map2);
        Map<String,Object>map3 = new HashMap<>();
        map3.put("value",234);
        map3.put("name","联盟广告");
        setmealCount.add(map3);
        Map<String,Object>map4 = new HashMap<>();
        map4.put("value",135);
        map4.put("name","视频广告");
        setmealCount.add(map4);
        Map<String,Object>map5 = new HashMap<>();
        map5.put("value",1548);
        map5.put("name","搜索引擎");
        setmealCount.add(map5);
        map.put("setmealCount",setmealCount);
        return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
*/

        List<Map<String,Object>> setmealCount =  setMealService.findSetmealCount();

//        System.out.println(setmealCount);

        map.put("setmealCount",setmealCount);

        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map1 : setmealCount) {
            String name = (String) map1.get("name");
            setmealNames.add(name);
        }
        map.put("setmealNames",setmealNames);
        return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
    }


    //统计运营数据
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){

        try {
            Map<String, Object> map = reportService.getBusinessReportData();

            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    //导出Excel表格
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){


        try {
            //获取数据
            Map<String, Object> map = reportService.getBusinessReportData();
            String reportDate = (String) map.get("reportDate");

            Integer todayNewMember = (Integer) map.get("todayNewMember");
            Integer totalMember = (Integer) map.get("totalMember");
            Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");

            List<Map<String, Object>> hotSetmeal = (List<Map<String, Object>>) map.get("hotSetmeal");


            //获取模板Excel
            String path = request.getSession().getServletContext().getRealPath("template")+ File.separator + "report_template.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(path));

            //获取第一个工作表
            XSSFSheet sheet = workbook.getSheetAt(0);
            //填充日期
            XSSFRow row = sheet.getRow(2);
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(reportDate);

            //填充新增会员数
            XSSFRow row1 = sheet.getRow(4);
            row1.getCell(5).setCellValue(todayNewMember);
            //填充总会员数
            row1.getCell(7).setCellValue(totalMember);

            //填充本周新增会员数
            XSSFRow row2 = sheet.getRow(5);
            row2.getCell(5).setCellValue(thisWeekNewMember);
            //填充本月新增会员数
            row2.getCell(7).setCellValue(thisMonthNewMember);

            //今日预约数,今日到诊数
            XSSFRow row3 = sheet.getRow(7);
            row3.getCell(5).setCellValue(todayOrderNumber);
            row3.getCell(7).setCellValue(todayVisitsNumber);

            //本周预约数,本周到诊数
            XSSFRow row4 = sheet.getRow(8);
            row4.getCell(5).setCellValue(thisWeekOrderNumber);
            row4.getCell(7).setCellValue(thisWeekVisitsNumber);

            //本月预约数,本月到诊数
            XSSFRow row5 = sheet.getRow(9);
            row5.getCell(5).setCellValue(thisMonthOrderNumber);
            row5.getCell(7).setCellValue(thisMonthVisitsNumber);

            //套餐一信息
            XSSFRow row6 = sheet.getRow(12);
            row6.getCell(4).setCellValue((String) hotSetmeal.get(0).get("name"));
            row6.getCell(5).setCellValue((Long) hotSetmeal.get(0).get("setmeal_count"));
            BigDecimal proportion1 = (BigDecimal) hotSetmeal.get(0).get("proportion");
            row6.getCell(6).setCellValue(proportion1.doubleValue());

            //套餐二信息
            XSSFRow row7 = sheet.getRow(13);
            row7.getCell(4).setCellValue((String) hotSetmeal.get(1).get("name"));
            row7.getCell(5).setCellValue((Long) hotSetmeal.get(1).get("setmeal_count"));
            BigDecimal proportion2 = (BigDecimal) hotSetmeal.get(1).get("proportion");
            row7.getCell(6).setCellValue(proportion2.doubleValue());

            //套餐三信息
            XSSFRow row8 = sheet.getRow(14);
            row8.getCell(4).setCellValue((String) hotSetmeal.get(2).get("name"));
            row8.getCell(5).setCellValue((Long) hotSetmeal.get(2).get("setmeal_count"));
            BigDecimal proportion3 = (BigDecimal) hotSetmeal.get(2).get("proportion");
            row8.getCell(6).setCellValue(proportion3.doubleValue());

            //套餐四信息
            XSSFRow row9 = sheet.getRow(15);
            row9.getCell(4).setCellValue((String) hotSetmeal.get(3).get("name"));
            row9.getCell(5).setCellValue((Long) hotSetmeal.get(3).get("setmeal_count"));
            BigDecimal proportion4 = (BigDecimal) hotSetmeal.get(3).get("proportion");
            row9.getCell(6).setCellValue(proportion4.doubleValue());

            ServletOutputStream outputStream = response.getOutputStream();
            //设置响应头,以附件下载的形式
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");
            //将Excel表格输出
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}
